package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivity;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsCategory.Column;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityGoodsCategoryDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityGoodsCategoryDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityService;
import com.google.common.collect.Lists;

@GraphQL2Module(type = Admin, description = "地产项目活动商品分类管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityGoodsCategoryGraphql.AUTH, name = "地产项目活动商品分类管理")
public class EstateProjectActivityGoodsCategoryGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-GOODS-CATEGORY";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	@Reference
	private EstateProjectActivityService estateProjectActivityService;
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "canDelete",description = "能删除")
	public boolean canDelete(@Source EstateProjectActivityGoodsCategory source, AdminMemberAuthority user) {
		return user.auth(AUTH_DEL);
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "canOnline",description = "能上线")
	public boolean canOnline(@Source EstateProjectActivityGoodsCategory source, AdminMemberAuthority user) {
		return !source.getOnline() && user.auth(AUTH_ONLINE);
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "canOffline",description = "能下线")
	public boolean canOffline(@Source EstateProjectActivityGoodsCategory source, AdminMemberAuthority user) {
		return source.getOnline() && user.auth(AUTH_OFFLINE);
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "parentCategory",description = "父分类")
	public EstateProjectActivityGoodsCategory parentCategory(@Source EstateProjectActivityGoodsCategory source) {
		if (Check.notNullAndGtZero(source.getParentCategoryId())) {
			return estateProjectActivityService.getEstateProjectActivityGoodsCategory(source.getParentCategoryId());
		}
		return null;
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "childrenCategory",description = "子分类")
	public List<EstateProjectActivityGoodsCategory> childrenCategory(@Source EstateProjectActivityGoodsCategory source) {
		return estateProjectActivityService.childrenCategory(source.getActivityCategoryId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsCategory.class,field = "estateProjectActivity",description = "所属项目")
	public EstateProjectActivity estateProjectActivity(@Source EstateProjectActivityGoodsCategory source) {
		return estateProjectActivityService.get(source.getProjectId());
	}
	
	@QueryField(field = "estateProjectActivityGoodsCategory" , description = "地产项目活动商品分类详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目活动商品分类详情")
	public EstateProjectActivityGoodsCategory get(@NonNull Integer activityCategoryId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return estateProjectActivityService.getEstateProjectActivityGoodsCategory(activityCategoryId);
	}
	
	@QueryField(field = "estateProjectActivityGoodsCategoryList" , description = "地产项目活动商品分类列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目活动商品分类列表")
	public Pagination<EstateProjectActivityGoodsCategory> query(@NonNull Integer projectId,String activityName,Boolean online,Integer level,@NonNull Sorting[] sorts,@NonNull Page page, AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return estateProjectActivityService.queryEstateProjectActivityGoodsCategory(projectId,activityName,online, level, sorts, page);
	}
	
	@MutationField(field = "addEstateProjectActivityGoodsCategory" , description = "添加地产项目活动商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加地产项目活动商品分类")
	public EstateProjectActivityGoodsCategory add(@NonNull AddEstateProjectActivityGoodsCategoryDTO record, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		EstateProjectActivity estateProjectActivity = estateProjectActivityService.get(record.getProjectId());
		Assert.isTrue(Check.notNull(estateProjectActivity),ResultCode.VERIFY.build(1), "地产项目不存在");
		return estateProjectActivityService.addEstateProjectActivityGoodsCategory(record, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityGoodsCategory" , description = "编辑地产项目活动商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑地产项目活动商品分类")
	public EstateProjectActivityGoodsCategory edit(@NonNull EditEstateProjectActivityGoodsCategoryDTO record, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		EstateProjectActivity estateProjectActivity = estateProjectActivityService.get(record.getProjectId());
		Assert.isTrue(Check.notNull(estateProjectActivity),ResultCode.VERIFY.build(1), "地产项目不存在");
		EstateProjectActivityGoodsCategory old = estateProjectActivityService.getEstateProjectActivityGoodsCategory(record.getActivityCategoryId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目活动商品分类不存在");
		BeanUtil.copy(record,
            old,
            Lists.newArrayList(
                    Column.deleted,
                    Column.createUser,
                    Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return estateProjectActivityService.editEstateProjectActivityGoodsCategory(old, user.userName());
	}
	
	@MutationField(field = "delEstateProjectActivityGoodsCategory" , description = "删除地产项目活动商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除地产项目活动商品分类")
	public boolean delete(@NonNull Integer activityCategoryId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		EstateProjectActivityGoodsCategory old = estateProjectActivityService.getEstateProjectActivityGoodsCategory(activityCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "活动商品分类不存在");
		estateProjectActivityService.delEstateProjectActivityGoodsCategory(old, user.userName());
		return true;
	}
	
	@MutationField(field = "onlineEstateProjectActivityGoodsCategory" , description = "上线地产项目活动商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线地产项目活动商品分类")
	public boolean online(@NonNull Integer activityCategoryId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		EstateProjectActivityGoodsCategory old = estateProjectActivityService.getEstateProjectActivityGoodsCategory(activityCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "活动商品分类不存在");
		estateProjectActivityService.onlineEstateProjectActivityGoodsCategory(old, user.userName());
		return true;
	}
	
	@MutationField(field = "offlineEstateProjectActivityGoodsCategory" , description = "下线地产项目活动商品分类")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线地产项目活动商品分类")
	public boolean offline(@NonNull Integer activityCategoryId, AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		EstateProjectActivityGoodsCategory old = estateProjectActivityService.getEstateProjectActivityGoodsCategory(activityCategoryId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "活动商品分类不存在");
		estateProjectActivityService.offlineEstateProjectActivityGoodsCategory(old, user.userName());
		return true;
	}
}
